/**
 * Copyright 2015-2016 org.lychee.com 
 * All rights reserved.
 *
 * @project
 * @author pro
 * @version 1.0
 * @date 2016-03-07
 */
package com.eyaomall.wxmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.dao.AddressDao;
import com.eyaomall.wxmall.model.po.Address;
import com.eyaomall.wxmall.service.AddressService;
@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	private AddressDao addressDao;
	
	/**
	 * 根据ID查询我的收货地址
	 */
	public Address queryById(Integer id) {
		
		return addressDao.queryById(id);
	}
	

	/**
	 * 根据Id查询收货地址列表
	 */
	public List<Address> list(Integer memberId) {
		return addressDao.list(memberId);
	}

	/**
	 * 新增收货地址
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public int createAddress(Address address) {
		if("y".equalsIgnoreCase(address.getIsDefault())){
			List<Address> list = list(MemberHelper.getId());
			for(Address addr : list){
				if("y".equalsIgnoreCase(addr.getIsDefault())){
					addr.setIsDefault("n");
					addressDao.update(addr);
				}
			}
		}
		
		return addressDao.createAddress(address);
	}

	/**
	 * 修改收货地址
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateAddress(Address address) {
		if("y".equalsIgnoreCase(address.getIsDefault())){
			List<Address> list = list(MemberHelper.getId());
			for(Address addr : list){
				if("y".equalsIgnoreCase(addr.getIsDefault())){
					addr.setIsDefault("n");
					addressDao.update(addr);
				}
			}
		}
		
		return addressDao.updateAddress(address);
	}

	/**
	 * 删除收货地址
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean deleteAddress(Integer id) {
		
		return addressDao.deleteAddress(id);
	}

	/**
	 * 修改为默认地址
	 */
	public boolean updateIsDefault(Integer id) {
		
		return addressDao.updateIsDefault(id);
	}
	
	/**
	 * 根据Member取消默认地址
	 */
	public boolean updateCancel(Integer memberId){
		
		return addressDao.UpdateCancel(memberId);
	}
	
	/**
	 * 根据MemberId查询收货地址
	 */
	public Address queryDefaultByMemberId(Integer memberId){
		
		return addressDao.queryByMemberId(memberId);
	}

}
